package com.axesline.sun;
import java.util.Random;
public class getMaxMin_Number {
public static void main(String[] args) {
int[] arry = new int[20];
Random r = new Random();
//给数组 随机赋值
for (int i = 0; i < arry.length; i++) {
arry[i] = r.nextInt(100);
System.out.print(arry[i]+" ");
}
System.out.println();
System.out.println("降序排列的结果: ");
sort(arry);
System.out.println();
System.out.println("冒泡排序的结果: ");
maoPao(arry);
System.out.println();
System.out.println("选择排序的结果: ");
xuanZe(arry);
System.out.println();
System.out.println("插入排序的结果: ");
chaRu(arry);
System.out.println();
System.out.println("希尔排序的结果: ");
xiEr(arry);
System.out.println();
System.out.println("最大值最小值和值: ");
maxMinSum(arry);
}
//降序
private static void sort(int[] arry) {
int t=0;
for(int i=0;i<arry.length-1;i++){
for(int m=i+1;m<arry.length;m++) {
if(arry[i]>arry ){
t=arry[i];
arry[i]=arry ;
arry =t;
}
}
}
for(int i=0;i<arry.length;i++){
System.out.print(arry[arry.length-1-i]+" ");
}
}
//冒泡
public static void maoPao(int[] arry){
int temp =0;
for (int i = 0; i < arry.length-1; i++) {
for (int j = i+1; j < arry.length; j++) {
if(arry[i]>arry[j]){
temp = arry[j];
arry[j] = arry[i];
arry[i] = temp;
}
}
}
for (int i = 0; i < arry.length; i++) {
System.out.print(arry[i]+" ");
}
}
//选择
public static void xuanZe(int[] arry){
for (int i = 0; i < arry.length; i++) {
int lowerindex = i;
for (int j = i+1; j < arry.length; j++) {
if(arry[j]<arry[lowerindex]){
lowerindex = j;
}
}
int temp = arry[i];
arry[i] = arry[lowerindex];
arry[lowerindex] = temp;
}
for (int i = 0; i < arry.length; i++) {
System.out.print(arry[i]+" ");
}
}
//插入
public static void chaRu(int[] arry){
for (int i = 1; i < arry.length; i++) {
for (int j = i; j > 0; j--) {
if(arry[j]<arry[j-1]){
int temp = arry[j];
arry[j] = arry[j-1];
arry[j-1] = temp;
}
}
}
for (int i = 0; i < arry.length; i++) {
System.out.print(arry[i]+" ");
}
}
//希尔
public static void xiEr(int[] arry){
System.out.println("......");
}
//求最大最小值求和
public static void maxMinSum(int[] arry){
int min = arry[0];
int max = arry[0];
int sum = 0;
for (int i = 0; i < arry.length; i++) {
sum = sum + arry[i];
if(max<arry[i]){
max = arry[i];
}
if(min>arry[i]){
min = arry[i];
}
}
System.out.println("max = "+max);
System.out.println("min = "+min);
System.out.println("sum = "+sum);
}
}
java一维数组排序及算法
于 2016-08-02 09:30:05 首次发布